# -*- coding:utf-8 -*-
import Pluto,struct,codecs,fnmatch
fl=Pluto.walk('MessageText')
for fn in fl:
    if fnmatch.fnmatch(fn,'*.txt'):
        continue
    print fn
    fp=open(fn,'rb')
    tar=codecs.open(fn+'.txt','wb','utf16')
    fp.seek(0x24)
    inf_size=struct.unpack('>i',fp.read(4))[0]
    num=struct.unpack('>H',fp.read(2))[0]
    dat_start=0x20+inf_size
    for i in xrange(num):
        fp.seek(0x30+i*4)
        offset=struct.unpack('>i',fp.read(4))[0]+dat_start+8
        fp.seek(offset)
        string=[]
        while True:
            newoffset=fp.tell()
            char=fp.read(1)
            if ord(char)<80:
                if char=='\x00':
                    break
                elif char=='\x0a':
                    string.append('\r\n')
                elif char=='\x1a':
                    string.append('{%02x}{%02x}{%02x}{%02x}{%02x}'%(ord(char),ord(fp.read(1)),ord(fp.read(1)),ord(fp.read(1)),ord(fp.read(1))))
                else:
                    string.append(char)
            else:
                fp.seek(newoffset)
                word=fp.read(2).decode('sjis')
                string.append(word)
        tar.write('No.%d\r\n'%i+u'－'*18+'\r\n')
        tar.write(''.join(string)+'\r\n'+u'－'*18+'\r\n'*2)
    fp.close()
    tar.close()